package com.pa.chong.pachong.tool.dowork;

import com.pa.chong.pachong.tool.dao.PhotoTypeDao;
import com.pa.chong.pachong.tool.entity.PhotoType;
import com.pa.chong.pachong.tool.utils.DownloadImage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class PaTu extends Thread{

    private String imgType;

    private DownloadImage downloadImage ;

    private PhotoTypeDao  photoTypeDao ;

    private String url;


    public PaTu(String imgType, DownloadImage downloadImage, PhotoTypeDao photoTypeDao, String url) {
        this.imgType = imgType;
        this.downloadImage = downloadImage;
        this.photoTypeDao = photoTypeDao;
        this.url = url;
    }





    @Override
    public void run() {



        System.out.println(Thread.currentThread().getName());
        getContent();
    }

    public void getContent(){
            try {
                System.out.println(url);
                Document document = Jsoup.connect(url).timeout(2000).get();
                Elements select = document.select("[id=Tag_list]").select("ul").select("li");
                for (Element element:select
                ) {
                    String attr = element.select("a").attr("href");
                    System.out.println(attr);
                    getPic(attr);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

    }

    private void getPic(String attr) throws IOException {
        String netUrl = "http://www.27270.com/ent/";
        int i = attr.lastIndexOf("/");
        String nets = attr.split("ent/")[1];
        String zhong = nets.split("/")[0];
        String parentId = attr.substring(i+1,attr.lastIndexOf("."));
        String nainfen = attr.substring(i-4,i);
        String next = attr.substring(i+1);

        PhotoType photoType = new PhotoType();
        photoType.setParentId(parentId);
        photoType.setImgType(imgType);
        photoTypeDao.save(photoType);
        boolean flag = true;
        while (true){
            String url = netUrl+zhong+"/"+nainfen+"/"+next;
            Document document1 = Jsoup.connect(url).timeout(2000).get();
            String nnext = document1.select("[id=nl]").select("a").attr("href");

            String src = document1.select("[id=picBody]").select("p").select("a").select("img").attr("src");
            String title = document1.select("[class=warp oh]").select("h1").text();

            try {
                System.out.println(src+"TTTTT"+Thread.currentThread().getName());
//                downloadImage.download(src,title,"",0,title,parentId,flag);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if("##".equals(nnext)){
                break;
            }
            next = nnext;
            System.out.println(url);
            flag = false;
            
        }

    }
}
